// 下载json
function downLoadJson (json: any, fileName: string) {
  let data = ''
  try {
    data = JSON.stringify(json, undefined, 4)
  } catch (error) {
    data = '{}'
  }
  fileName = fileName || 'code.json'
  if (typeof data === 'object') {
    // 将json转化为字符串
    data = JSON.stringify(data, undefined, 4)
  }
  const blob = new Blob([data], { type: 'text/json' })
  const e = document.createEvent('MouseEvents')
  const a = document.createElement('a')
  a.download = fileName
  a.href = window.URL.createObjectURL(blob)
  a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
  // 模拟下载文件
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  a.dispatchEvent(e)
}
export default {
  'down-json': downLoadJson
}
